<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Models\Admin\Tag;

class TagController extends Controller
{
    protected $fields = [
        'tag_name'  => '',
    ];
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Tag $tag_mod)
    {
        $article_num = $tag_mod->getArticleNum();
        $data = array();
        foreach($article_num as $k=>$v){
            $data[$v->tag_id] = $v->article_num;
        }
        $tag = $tag_mod->orderby('id','ASC')->Paginate(15);
        return view('admin.tag.index',compact('tag','data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $data = array();
        foreach ($this->fields as $field => $default) {
            $data[$field] = old($field, $default);
        }
        return view('admin.tag.create',$data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Requests\AdminTagRequest $request,Tag $tag_mod)
    {
        foreach (array_keys($this->fields) as $field) {
            $tag_mod->$field = $request->get($field);
        }
        $tag_mod->save();
        event(new \App\Events\userActionEvent('\App\Models\Admin\AdminTag', $tag_mod->id, 1, '添加标签' . $tag_mod->tag_name));
        return redirect('/admin/tag')->withSuccess('添加成功！');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id,Tag $tag_mod)
    {
        $tag = $tag_mod->find((int)$id);
        event(new \App\Events\userActionEvent('\App\Models\Admin\AdminTag', $tag->id, 3, '编辑了标签' . $tag->tag_name));
        return view('admin.tag.edit', $tag);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Tag $tag_mod,$id)
    {
        $tag = $tag_mod->find((int)$id);
        foreach (array_keys($this->fields) as $field) {
            $tag->$field = $request->get($field);
        }
        $tag->save();
        return redirect('/admin/tag')->withSuccess('编辑成功！');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Tag $tag_mod,$id)
    {
        $tag = $tag_mod->find((int)$id);
        $tag->delete();
        return redirect()->back()
            ->withSuccess("删除成功");
    }
}
